package com.lh.web.controller.order;

import com.lh.common.entity.admin.*;
import com.lh.common.entity.state.RemarkEnum;
import com.lh.common.enums.OrderStatusEnum;
import com.lh.common.enums.OrderTypeEnum;
import com.lh.common.enums.TrueFalseEnum;
import com.lh.common.enums.UserTypeEnum;
import com.lh.common.utils.*;
import com.lh.web.controller.AbstractController;
import com.lh.web.service.serviceAdmin.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author FH
 * @ClassName: OrderAction
 * @Description: (订单管理)
 * @date: 2020-05-18 9:33
 */
@Api(value="OrderAction",tags={"订单管理"})
@RestController
@RequestMapping("order")
public class OrderAction extends AbstractController {

    @Autowired
    private OrderService orderService;

    /**
     * 查询一元夺宝订单列表页面
     */
    @ApiOperation(value = "查询一元夺宝订单列表页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "下单开始时间", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "下单结束时间", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "orderStatus", value = "订单状态", required = false, dataTypeClass= OrderStatusEnum.class),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "phone", value = "手机号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "userType", value = "用户类型", required = false, dataTypeClass= UserTypeEnum.class),
            @ApiImplicitParam(paramType="query", name = "shopName", value = "活动名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "period", value = "期数", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "result", value = "开奖结果", required = false, dataTypeClass= TrueFalseEnum.class)
    })
    @GetMapping("/getOrderOneList")
    @RequiresPermissions("order:getOrderOneList")
    public Result<OrderOneList> getOrderOneList(String orderItem, String startTime, String endTime, OrderStatusEnum orderStatus, String name, String phone, UserTypeEnum userType, String shopName, String period, TrueFalseEnum result, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("name", name);
        params.put("phone", phone);
        params.put("orderItem", orderItem);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("shopName", shopName);
        params.put("period", period);
        if(result != null) {
            params.put("result", result.getValue());
        }
        if(orderStatus != null) {
            params.put("orderStatus", orderStatus.getValue());
        }
        if(userType != null) {
            params.put("userType", userType.getValue());
        }
        PageUtils pageUtils = orderService.queryOrderList(params, pageNum, pageSize);
        return Result.ok(pageUtils);
    }

    /**
     * 一元夺宝订单详情页面
     */
    @ApiOperation(value = "一元夺宝订单详情页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderId", value = "订单ID", required = true, dataTypeClass= Long.class),
    })
    @GetMapping("/getOrderOneDetails")
    @RequiresPermissions("order:getOrderOneDetails")
    public Result<OrderOneDetail> getOrderOneDetails(Long orderId){
        AssertUtil.assertNotNull(orderId, Constant.RESULT.FI1000, "未选择订单ID!");
        OrderOneDetail orderOneDetail = orderService.queryOrderOneDetails(orderId);
        return Result.ok(orderOneDetail);
    }

    /**
     * 一元活动导出订单
     */
    @ApiOperation(value = "一元活动导出订单")
    @GetMapping("/exportOrderOne")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "下单开始时间", required = true, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "下单结束时间", required = true, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "orderStatus", value = "订单状态", required = false, dataTypeClass= OrderStatusEnum.class),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "phone", value = "手机号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "userType", value = "用户类型", required = false, dataTypeClass= UserTypeEnum.class),
            @ApiImplicitParam(paramType="query", name = "shopName", value = "活动名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "period", value = "期数", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "result", value = "开奖结果", required = false, dataTypeClass= TrueFalseEnum.class)
    })
    @RequiresPermissions("order:exportOrderOne")
    public void exportOrderOne(String orderItem, String startTime, String endTime, OrderStatusEnum orderStatus, String name, String phone, UserTypeEnum userType, String shopName, String period, TrueFalseEnum result, HttpServletResponse response) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        AssertUtil.assertNotNull(startTime, Constant.RESULT.FI1000, "未选择下单开始时间!");
        AssertUtil.assertNotNull(endTime, Constant.RESULT.FI1000, "未选择下单结束时间!");
        params.put("orderItem", orderItem);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("name", name);
        params.put("period", period);
        params.put("phone", phone);
        params.put("shopName", shopName);
        if(orderStatus != null) {
            params.put("orderStatus", orderStatus.getValue());
        }
        if(result != null) {
            params.put("result", result.getValue());
        }
        if(userType != null) {
            params.put("userType", userType.getValue());
        }
        PageUtils pageUtils = orderService.queryOrderList(params, 0, Integer.MAX_VALUE);
        ExcelUtilJXL.exportExcelToBrowserForJSP(OrderOneList.class, pageUtils.getList(),"一元夺宝订单.xls",response);
    }

    /**
     * 查询普通订单列表页面
     */
    @ApiOperation(value = "查询普通订单列表页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "当前页", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页数大小", required = true,dataTypeClass= Integer.class),
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "下单开始时间", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "下单结束时间", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "orderStatus", value = "订单状态", required = false, dataTypeClass= OrderStatusEnum.class),
            @ApiImplicitParam(paramType="query", name = "shopName", value = "商品名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "phone", value = "手机号", required = false, dataTypeClass= String.class)
    })
    @GetMapping("/getOrderList")
    @RequiresPermissions("order:getOrderList")
    public Result<OrderList> getOrderList(String orderItem, String startTime, String endTime, OrderStatusEnum orderStatus, String name, String phone, String shopName, Integer pageNum, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageNum", pageNum);
        params.put("pageSize", pageSize);
        params.put("name", name);
        params.put("orderItem", orderItem);
        params.put("endTime", endTime);
        params.put("startTime", startTime);
        params.put("phone", phone);
        params.put("shopName", shopName);
        if(orderStatus != null) {
            params.put("orderStatus", orderStatus.getValue());
        }
        PageUtils pageUtils = orderService.getOrderList(params, pageNum, pageSize);
        return Result.ok(pageUtils);
    }

    /**
     * 订单出库
     */
    @ApiOperation(value = "订单出库")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderId", value = "订单ID", required = true, dataTypeClass= Long.class)
    })
    @GetMapping("/updateOrderWarehouse")
    @RequiresPermissions("order:updateOrderWarehouse")
    public Result updateOrderWarehouse(Long orderId){
        try {
            OrderDetail order = orderService.queryOrderDetails(orderId);
            if(!order.getOrderStatus().equals(OrderStatusEnum.TOSHIP) && order.getOrderType().equals(OrderTypeEnum.ORDINARYSHOP)){
                return Result.error("订单状态不是已付款状态");
            }
            Long userId = getUserId();
            orderService.updateOrderWarehouse(userId,orderId,OrderStatusEnum.TOSHIP.getValue(),OrderStatusEnum.OUTSTOCK.getValue(), RemarkEnum.SHZT13);
            return Result.ok();
        }catch (Exception e){
            return Result.error();
        }
    }

    /**
     * 订单发货
     */
    @ApiOperation(value = "订单发货")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderId", value = "订单ID", required = true, dataTypeClass= Long.class),
            @ApiImplicitParam(paramType="query", name = "expressName", value = "物流公司", required = true, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "expressCode", value = "物流单号", required = true, dataTypeClass= String.class),
    })
    @GetMapping("/updateOrderDeliver")
    @RequiresPermissions("order:updateOrderDeliver")
    public Result updateOrderDeliver(Long orderId,String expressName,String expressCode){
        try {
            OrderDetail order = orderService.queryOrderDetails(orderId);
            if(!order.getOrderStatus().equals(OrderStatusEnum.OUTSTOCK) && order.getOrderType().equals(OrderTypeEnum.ORDINARYSHOP)){
                return Result.error("订单状态不是已付款状态");
            }
            Long userId = getUserId();
            orderService.updateOrderWarehouse(userId,orderId,expressName,expressCode,OrderStatusEnum.OUTSTOCK.getValue(),OrderStatusEnum.SHIPED.getValue(), RemarkEnum.SHZT14);
            return Result.ok();
        }catch (Exception e){
            return Result.error();
        }
    }

    /**
     * 普通订单详情页面
     */
    @ApiOperation(value = "普通订单详情页面")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderId", value = "订单ID", required = true, dataTypeClass= Long.class),
    })
    @GetMapping("/getOrderDetails")
    @RequiresPermissions("order:getOrderDetails")
    public Result<OrderDetail> getOrderDetails(Long orderId){
        AssertUtil.assertNotNull(orderId, Constant.RESULT.FI1000, "未选择订单ID!");
        OrderDetail orderDetail = orderService.queryOrderDetails(orderId);
        return Result.ok(orderDetail);
    }

    /**
     * 普通导出订单
     */
    @ApiOperation(value = "普通导出订单")
    @GetMapping("/exportOrder")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "orderItem", value = "订单号", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "startTime", value = "下单开始时间", required = true, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "endTime", value = "下单结束时间", required = true, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "orderStatus", value = "订单状态", required = false, dataTypeClass= OrderStatusEnum.class),
            @ApiImplicitParam(paramType="query", name = "shopName", value = "商品名称", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "name", value = "用户名", required = false, dataTypeClass= String.class),
            @ApiImplicitParam(paramType="query", name = "phone", value = "手机号", required = false, dataTypeClass= String.class)
    })
    @RequiresPermissions("order:exportOrder")
    public void exportOrder(String orderItem, String startTime, String endTime, OrderStatusEnum orderStatus, String name, String phone, String shopName,HttpServletResponse response) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        AssertUtil.assertNotNull(startTime, Constant.RESULT.FI1000, "未选择下单开始时间!");
        AssertUtil.assertNotNull(endTime, Constant.RESULT.FI1000, "未选择下单结束时间!");
        params.put("orderItem", orderItem);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        params.put("name", name);
        params.put("phone", phone);
        params.put("shopName", shopName);
        if(orderStatus != null) {
            params.put("orderStatus", orderStatus.getValue());
        }
        PageUtils pageUtils = orderService.getOrderList(params, 0, Integer.MAX_VALUE);
        ExcelUtilJXL.exportExcelToBrowserForJSP(OrderList.class, pageUtils.getList(),"普通导出订单.xls",response);
    }

}
